package cn.lyjuan.pojoutils.util;

import cn.lyjuan.pojoutils.bean.DBType;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Created by ly on 2014/11/20.
 */
public class ConnUtils
{
    public static void loadDBDriver(String driver)
    {
        try
        {
            Class.forName(driver);
        } catch (ClassNotFoundException e)
        {
            throw new RuntimeException(e);
        }
    }

    public static Connection getConn(String url, String user, String pwd)
    {
        Connection conn = null;
        try
        {
            conn = DriverManager.getConnection(url, user, pwd);
        } catch (SQLException e)
        {
            throw new RuntimeException(e);
        }

        return conn;
    }

    /**
     * 判断当前使用的数据库类型
     * @return
     */
    public static DBType getDBType(String driver)
    {
        if (driver.toLowerCase().indexOf("oracle") > -1)
            return DBType.ORACLE;

        if (driver.toLowerCase().indexOf("mysql") > -1)
            return DBType.MYSQL;

        throw new RuntimeException("nonsupport database type");
    }
}
